package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author AXiang
 * @create 2023/10/27 15:24
 **/
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setMealService.saveWithDish(setmealDTO);
        return Result.success();
    }
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult=setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @ApiOperation("套餐批量删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public  Result delete( @RequestParam List<Long> ids){
        setMealService.deleteBatch(ids);
        return Result.success();
    }
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getByiId(@PathVariable  Long id){
        SetmealVO setmealVO=setMealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }
    @ApiOperation("套餐修改")
    @PutMapping
    @CacheEvict(cacheNames = "setMealCache",key = "#setmealDTO.categoryId")
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setMealService.update(setmealDTO);
        return Result.success();
    }
    @ApiOperation("套餐起售停售状态的修改")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result starOrStop(@PathVariable Integer status,@RequestParam Long id){
        setMealService.starOrStop(status,id);
        return Result.success();
    }
}
